<template>
    <div>
        <input type="file" @change="onChange" />
        这是缺少，该文件{{ file.length }}条
        <br>
        <input type="file" @change="onChange2" />
        这是完整文件，该文件{{ file2.length }}条
        <br>
        <button @click="duibi">对比</button>
        <button @click="exportToExcel">导出</button>
    </div>
</template>

<script>
const xlsx = require("xlsx");

export default {
    data() {
        return {
            file: [],
            file2: [],
        };
    },
    methods: {
        exportToExcel() {
            const data = this.missingValues.map(item => [item]); // 将每一项作为一行
            const worksheet = xlsx.utils.aoa_to_sheet(data);
            const workbook = xlsx.utils.book_new();
            xlsx.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
            const excelBuffer = xlsx.write(workbook, { bookType: 'xlsx', type: 'array' });
            const blob = new Blob([excelBuffer], { type: 'application/octet-stream' });
            const url = window.URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = `缺少的号码${new Date().getTime()}.xlsx` ;
            a.click();
            window.URL.revokeObjectURL(url);
        },
        duibi() {
            const setB = new Set(this.file);
            const missingValues = this.file2.filter(item => !setB.has(item));
            console.log('缺少的值:', missingValues);
            this.missingValues = missingValues;
        },
        onChange(event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = (e) => {
                    const data = new Uint8Array(e.target.result);
                    const workbook = xlsx.read(data, { type: 'array' });
                    const firstSheetName = workbook.SheetNames[0];
                    const worksheet = workbook.Sheets[firstSheetName];
                    const json = xlsx.utils.sheet_to_json(worksheet, { header: 1 });
                    console.log(json);
                    json.forEach(element => {
                        this.file.push(element[0]);
                    });
                    console.log(this.file)
                };
                reader.readAsArrayBuffer(file);
            }
        },
        onChange2(event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = (e) => {
                    const data = new Uint8Array(e.target.result);
                    const workbook = xlsx.read(data, { type: 'array' });
                    const firstSheetName = workbook.SheetNames[0];
                    const worksheet = workbook.Sheets[firstSheetName];
                    const json = xlsx.utils.sheet_to_json(worksheet, { header: 1 });
                    console.log(json)
                    json.forEach(element => {
                        this.file2.push(element[0]);
                    });
                    console.log(this.file2)
                };
                reader.readAsArrayBuffer(file);
            }
        }
    },
};
</script>